home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / util / misc / ReportPlus.lha / reportplus / source / f7.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-25  |  17.5 KB  |  567 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <intuition/intuition.h>
  4. #include <intuition/gadgetclass.h>
  5. #include <libraries/gadtools.h>
  6. #include <dos/dosextens.h>
  7. #include <graphics/gfx.h>
  8.  
  9. #include <clib/exec_protos.h>
  10. #include <clib/intuition_protos.h>
  11. #include <clib/gadtools_protos.h>
  12. #include <clib/dos_protos.h>
  13. #include <clib/graphics_protos.h>
  14.  
  15. #include <ctype.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "rp.h"
  19.  
  20. #define AMIGA             0
  21. #define MAC               1
  22. #define IBM               2
  23.  
  24. MODULE void updatedetabulate(void);
  25.  
  26. MODULE  struct Gadget *MX71_Source      = NULL,
  27.                       *TE71_SourceTitle = NULL,
  28.                       *MX71_Dest        = NULL,
  29.                       *TE71_DestTitle   = NULL,
  30.                       *ST71_Pathname    = NULL,
  31.                       *BU71_PathnameASL = NULL,
  32.                       *CB71_Detabulate  = NULL,
  33.                       *IN71_TabSize     = NULL,
  34.                       *BU71_Convert     = NULL;
  35. AGLOBAL struct Gadget* TE71_Status      = NULL;
  36.  
  37. // from rp.c
  38. IMPORT ABOOL               fillwindows, stop;
  39. IMPORT SBYTE               page;
  40. IMPORT SWORD               y;
  41. IMPORT TEXT                asldir[VLONGFIELD + 1];
  42. IMPORT ULONG               fillcolour;
  43. IMPORT struct NewGadget    Gadget;
  44. IMPORT struct Gadget      *BU99_Right,
  45.                           *BU99_Stop,
  46.                           *PrevGadPtr;
  47. IMPORT struct Window*      MainWindowPtr;
  48. IMPORT struct Screen*      ScreenPtr;
  49. IMPORT struct IconBase*    IconBase;
  50. IMPORT struct SharedStruct shared;
  51.  
  52. AGLOBAL struct EOLStruct eol =
  53. {   FALSE, IBM, AMIGA, 8
  54. };
  55.  
  56. // EOL/tab converter
  57. MODULE  STRPTR EOLOptions[4] =
  58. {   (STRPTR) "LF    (Amiga)",
  59.     (STRPTR) "CR    (Macintosh)",
  60.     (STRPTR) "LF+CR (IBM-PC)",
  61.     NULL
  62. };
  63.  
  64. AGLOBAL void eol1(void)
  65. {   shared.pathname[0] = 0;
  66.  
  67.     verynewwindow
  68.     (   EOL1WIDTH, EOL1HEIGHT,
  69.         "Report+: EOL/Tab Converter",
  70.         BUTTONIDCMP | STRINGIDCMP | MXIDCMP
  71.     );
  72.     if (fillwindows)
  73.     {   SetAPen(MainWindowPtr->RPort, 0);
  74.         RectFill(MainWindowPtr->RPort, 80, 134, 80 + EOL1WIDTH - 160, 134 + 12); // TE71_Status
  75.     }
  76.  
  77.     /* source title */
  78.     setgadget(62 + 2, 40, 100, 12, "_Source:", PLACETEXT_ABOVE);
  79.     // be sure this never overlaps with MX71_Source
  80.     TE71_SourceTitle = PrevGadPtr = (struct Gadget *) CreateGadget
  81.     (   TEXT_KIND,
  82.         PrevGadPtr,
  83.         &Gadget,
  84.         GT_Underscore, '_',
  85.         TAG_DONE
  86.     );
  87.     /* source format */
  88.     setgadget(62, 50, 0, 0, NULL, PLACETEXT_RIGHT);
  89.     MX71_Source = PrevGadPtr = (struct Gadget *) CreateGadget
  90.     (   MX_KIND,
  91.         PrevGadPtr,
  92.         &Gadget,
  93.         GTMX_Labels, &EOLOptions,
  94.         GTMX_Active, eol.source,
  95.         TAG_DONE
  96.     );
  97.     /* dest title */
  98.     setgadget(314 + 22, 40, 100, 12, "_Destination:", PLACETEXT_ABOVE);
  99.     // be sure this never overlaps with MX71_Dest
  100.     TE71_DestTitle = PrevGadPtr = (struct Gadget *) CreateGadget
  101.     (   TEXT_KIND,
  102.         PrevGadPtr,
  103.         &Gadget,
  104.         GT_Underscore, '_',
  105.         TAG_DONE
  106.     );
  107.     /* destination format */
  108.     setgadget(314, 50, 0, 0, NULL, PLACETEXT_RIGHT);
  109.     MX71_Dest = PrevGadPtr = (struct Gadget *) CreateGadget
  110.     (   MX_KIND,
  111.         PrevGadPtr,
  112.         &Gadget,
  113.         GTMX_Labels, &EOLOptions,
  114.         GTMX_Active, eol.dest,
  115.         TAG_DONE
  116.     );
  117.  
  118.     /* pathname */
  119.     setgadget(132, 88, 340, 12, "_Pathname(s):", NULL);
  120.     ST71_Pathname = PrevGadPtr = (struct Gadget *) CreateGadget
  121.     (   STRING_KIND,
  122.         PrevGadPtr,
  123.         &Gadget,
  124.         GTST_String, shared.pathname,
  125.         GTST_MaxChars, LONGESTFIELD,
  126.         GA_TabCycle, TRUE,
  127.         GT_Underscore, '_',
  128.         TAG_DONE
  129.     );
  130.     /* pathname... */
  131.     setgadget(474, 88, 28, 12, "_...", NULL);
  132.     BU71_PathnameASL = PrevGadPtr = (struct Gadget *) CreateGadget
  133.     (   BUTTON_KIND,
  134.         PrevGadPtr,
  135.         &Gadget,
  136.         GT_Underscore, '_',
  137.         TAG_DONE
  138.     );
  139.  
  140.     // 'convert tabs to spaces?'
  141.     setgadget(294, 110, 0, 0, "C_onvert tabs to spaces?", NULL);
  142.     CB71_Detabulate = PrevGadPtr = (struct Gadget *) CreateGadget
  143.     (   CHECKBOX_KIND,
  144.         PrevGadPtr,
  145.         &Gadget,
  146.         GTCB_Checked, eol.detabulate,
  147.         GT_Underscore, '_',
  148.         TAG_DONE
  149.     );
  150.     /* tab size */
  151.     setgadget(408, 110, 32, 12, "_Tab size:", NULL);
  152.     IN71_TabSize = PrevGadPtr = (struct Gadget *) CreateGadget
  153.     (   INTEGER_KIND,
  154.         PrevGadPtr,
  155.         &Gadget,
  156.         GTIN_Number, eol.tabsize,
  157.         GTIN_MaxChars, 2,
  158.         GA_TabCycle, TRUE,
  159.         GT_Underscore, '_',
  160.         TAG_DONE
  161.     );
  162.     setgadget(80, 134, EOL1WIDTH - 160, 12, "Status:", NULL);
  163.     TE71_Status = PrevGadPtr = (struct Gadget *) CreateGadget
  164.     (   TEXT_KIND,
  165.         PrevGadPtr,
  166.         &Gadget,
  167.         GTTX_Text, "Ready.",
  168.         GTTX_Border, TRUE,
  169.         TAG_DONE
  170.     );
  171.     setgadget(10, EOL1HEIGHT - 28, (EOL1WIDTH / 2) - 10, 12, "_Convert", NULL);
  172.     BU71_Convert = PrevGadPtr = (struct Gadget *) CreateGadget
  173.     (   BUTTON_KIND,
  174.         PrevGadPtr,
  175.         &Gadget,
  176.         GT_Underscore, '_',
  177.         TAG_DONE
  178.     );
  179.     setgadget(EOL1WIDTH / 2, EOL1HEIGHT - 28, (EOL1WIDTH / 2) - 10, 12, "Stop", NULL);
  180.     BU99_Stop = PrevGadPtr = (struct Gadget *) CreateGadget
  181.     (   BUTTON_KIND,
  182.         PrevGadPtr,
  183.         &Gadget,
  184.         GT_Underscore, '_',
  185.         GA_Disabled, TRUE,
  186.         TAG_DONE
  187.     );
  188.  
  189.     drawgadgets((UWORD) ~0);
  190.     ActivateGadget(ST71_Pathname, MainWindowPtr, NULL);
  191.     if (eol.detabulate)
  192.     {   GT_SetGadgetAttrs
  193.         (   IN71_TabSize,
  194.             MainWindowPtr, NULL,
  195.             GA_Disabled, FALSE,
  196.             TAG_DONE
  197.         );
  198.     } else
  199.     {   GT_SetGadgetAttrs
  200.         (   IN71_TabSize,
  201.             MainWindowPtr, NULL,
  202.             GA_Disabled, TRUE,
  203.             TAG_DONE
  204.         );
  205.     }
  206.     loop();
  207.     closewindow();
  208. }
  209.  
  210. AGLOBAL void eol_do(void)
  211. {   strcpy
  212.     (   shared.pathname,
  213.         ((struct StringInfo *) ST71_Pathname->SpecialInfo)->Buffer
  214.     );
  215.     eol.tabsize = ((struct StringInfo *) IN71_TabSize->SpecialInfo)->LongInt;
  216.  
  217.     GT_SetGadgetAttrs
  218.     (   BU99_Stop,
  219.         MainWindowPtr, NULL,
  220.         GA_Disabled, FALSE,
  221.         TAG_DONE
  222.     );
  223.     GT_SetGadgetAttrs
  224.     (   MX71_Source,
  225.         MainWindowPtr, NULL,
  226.         GA_Disabled, TRUE,
  227.         TAG_DONE
  228.     );
  229.     GT_SetGadgetAttrs
  230.     (   MX71_Dest,
  231.         MainWindowPtr, NULL,
  232.         GA_Disabled, TRUE,
  233.         TAG_DONE
  234.     );
  235.     GT_SetGadgetAttrs
  236.     (   ST71_Pathname,
  237.         MainWindowPtr, NULL,
  238.         GA_Disabled, TRUE,
  239.         TAG_DONE
  240.     );
  241.     GT_SetGadgetAttrs
  242.     (   BU71_PathnameASL,
  243.         MainWindowPtr, NULL,
  244.         GA_Disabled, TRUE,
  245.         TAG_DONE
  246.     );
  247.     GT_SetGadgetAttrs
  248.     (   CB71_Detabulate,
  249.         MainWindowPtr, NULL,
  250.         GA_Disabled, TRUE,
  251.         TAG_DONE
  252.     );
  253.     GT_SetGadgetAttrs
  254.     (   IN71_TabSize,
  255.         MainWindowPtr, NULL,
  256.         GA_Disabled, TRUE,
  257.         TAG_DONE
  258.     );
  259.     GT_SetGadgetAttrs
  260.     (   BU71_Convert,
  261.         MainWindowPtr, NULL,
  262.         GA_Disabled, TRUE,
  263.         TAG_DONE
  264.     );
  265.     GT_SetGadgetAttrs
  266.     (   BU99_Right,
  267.         MainWindowPtr, NULL,
  268.         GA_Disabled, TRUE,
  269.         TAG_DONE
  270.     );
  271.  
  272.     convert(TRUE);
  273.  
  274.     if (!stop)
  275.     {   GT_SetGadgetAttrs
  276.         (   TE71_Status,
  277.             MainWindowPtr, NULL,
  278.             GTTX_Text, "All done.",
  279.             TAG_DONE
  280.         );
  281.     }
  282.     GT_SetGadgetAttrs
  283.     (   BU99_Stop,
  284.         MainWindowPtr, NULL,
  285.         GA_Disabled, TRUE,
  286.         TAG_DONE
  287.     );
  288.     GT_SetGadgetAttrs
  289.     (   MX71_Source,
  290.         MainWindowPtr, NULL,
  291.         GA_Disabled, FALSE,
  292.         TAG_DONE
  293.     );
  294.     GT_SetGadgetAttrs
  295.     (   MX71_Dest,
  296.         MainWindowPtr, NULL,
  297.         GA_Disabled, FALSE,
  298.         TAG_DONE
  299.     );
  300.     GT_SetGadgetAttrs
  301.     (   ST71_Pathname,
  302.         MainWindowPtr, NULL,
  303.         GA_Disabled, FALSE,
  304.         TAG_DONE
  305.     );
  306.     GT_SetGadgetAttrs
  307.     (   BU71_PathnameASL,
  308.         MainWindowPtr, NULL,
  309.         GA_Disabled, FALSE,
  310.         TAG_DONE
  311.     );
  312.     GT_SetGadgetAttrs
  313.     (   CB71_Detabulate,
  314.         MainWindowPtr, NULL,
  315.         GA_Disabled, FALSE,
  316.         TAG_DONE
  317.     );
  318.     GT_SetGadgetAttrs
  319.     (   IN71_TabSize,
  320.         MainWindowPtr, NULL,
  321.         GA_Disabled, FALSE,
  322.         TAG_DONE
  323.     );
  324.     GT_SetGadgetAttrs
  325.     (   BU71_Convert,
  326.         MainWindowPtr, NULL,
  327.         GA_Disabled, FALSE,
  328.         TAG_DONE
  329.     );
  330.     GT_SetGadgetAttrs
  331.     (   BU99_Right,
  332.         MainWindowPtr, NULL,
  333.         GA_Disabled, FALSE,
  334.         TAG_DONE
  335.     );
  336. }
  337.  
  338. AGLOBAL void eolconvert(ABOOL gui)
  339. {   ULONG                gap, i, j, cursorx = 0;
  340.     ABOOL                ok                 = TRUE;
  341.     struct FileInfoBlock InputInfoBlock;
  342.     UBYTE*               InputBufferPtr; // always use UBYTE not APTR for pointer arithmetic
  343.     BPTR                 FileHandle;
  344.     TEXT                 saystring[LONGFIELD + 1];
  345.  
  346.     strcpy(saystring, "Converting ");
  347.     strcat(saystring, shared.thisfile);
  348.     strcat(saystring, "...");
  349.     if (gui)
  350.     {   GT_SetGadgetAttrs
  351.         (   TE71_Status,
  352.             MainWindowPtr, NULL,
  353.             GTTX_Text, saystring,
  354.             TAG_DONE
  355.         );
  356.     } else
  357.     {   Printf("%s", saystring);
  358.         Flush(Output());
  359.     }
  360.  
  361.     if (FileHandle = (BPTR) Lock(shared.thisfile, ACCESS_READ))
  362.     {   if (Examine(FileHandle, &InputInfoBlock))
  363.         {   UnLock(FileHandle);
  364.             // FileHandle = NULL;
  365.             if (InputInfoBlock.fib_DirEntryType == -3) // if a file
  366.             {   // if it is a directory, we would ideally like to convert all
  367.                 // files in that directory. We also should support links.
  368.                 if (InputBufferPtr = AllocMem(InputInfoBlock.fib_Size, MEMF_ANY | MEMF_PUBLIC))
  369.                 {   if (FileHandle = (BPTR) Open(shared.thisfile, MODE_OLDFILE))
  370.                     {   if (Read(FileHandle, InputBufferPtr, InputInfoBlock.fib_Size) != -1)
  371.                         {   Close(FileHandle);
  372.                             if (FileHandle = (BPTR) Open(shared.thisfile, MODE_NEWFILE))
  373.                             {   for (i = 0; i < InputInfoBlock.fib_Size; i++)
  374.                                 {   if (*(InputBufferPtr + i) == LF)
  375.                                     {   cursorx = 0;
  376.                                         if (eol.source == eol.dest)
  377.                                             FPutC(FileHandle, LF); // pass it through
  378.                                         elif (eol.source == AMIGA && eol.dest == IBM)
  379.                                         {   FPutC(FileHandle, CR); // add this
  380.                                             FPutC(FileHandle, LF); // and pass this through
  381.                                         } elif (eol.source == AMIGA && eol.dest == MAC)
  382.                                             FPutC(FileHandle, CR); // convert from LF to CR
  383.                                         elif (eol.source == IBM && eol.dest == AMIGA)
  384.                                             FPutC(FileHandle, LF); // pass it through
  385.                                         elif (eol.source == IBM && eol.dest == MAC)
  386.                                             ; // do nothing
  387.                                         else ; // mac as source: we never expect LFs
  388.                                     } elif (*(InputBufferPtr + i) == CR)
  389.                                     {   cursorx = 0;
  390.                                         if (eol.source == eol.dest)
  391.                                             FPutC(FileHandle, CR); // pass it through
  392.                                         elif (eol.source == IBM && eol.dest == AMIGA)
  393.                                             ; // do nothing
  394.                                         elif (eol.source == IBM && eol.dest == MAC)
  395.                                             FPutC(FileHandle, CR); // pass it through
  396.                                         elif (eol.source == MAC && eol.dest == AMIGA)
  397.                                             FPutC(FileHandle, LF); // convert from CR to LF
  398.                                         elif (eol.source == MAC && eol.dest == IBM)
  399.                                         {   FPutC(FileHandle, CR); // pass this through
  400.                                             FPutC(FileHandle, LF); // and add this
  401.                                         } else ; // amiga as source: we never expect CRs
  402.                                     } elif (*(InputBufferPtr + i) == TAB)
  403.                                     {   if (eol.detabulate)
  404.                                         {   gap = (((cursorx / eol.tabsize) + 1) * eol.tabsize) - cursorx;
  405.                                             for (j = 1; j <= gap; j++)
  406.                                                 FPutC(FileHandle, ' ');
  407.                                             cursorx += gap;
  408.                                         } else FPutC(FileHandle, TAB); // no point to cursorx in non-detabulate
  409.                                     } else
  410.                                     {   FPutC(FileHandle, *(InputBufferPtr + i));
  411.                                         cursorx++;
  412.                                 }   }
  413.                                 /* Note that FPutC() returns EOF for errors; this is not currently
  414.                                 checked. */
  415.                                 ok = TRUE;
  416.                                 Close(FileHandle);
  417.                         }   }
  418.                         else
  419.                         {   Close(FileHandle);
  420.                     }   }
  421.                     FreeMem(InputBufferPtr, InputInfoBlock.fib_Size);
  422.         }   }   }
  423.         else
  424.         {   UnLock(FileHandle);
  425.     }   }
  426.  
  427.     if (gui)
  428.     {   if (ok)
  429.             strcat(saystring, "done.");
  430.         else strcat(saystring, "failed!");
  431.         GT_SetGadgetAttrs
  432.         (   TE71_Status,
  433.             MainWindowPtr, NULL,
  434.             GTTX_Text, saystring,
  435.             TAG_DONE
  436.         );
  437.     } else
  438.     {   if (ok)
  439.             Printf("done.\n");
  440.         else Printf("failed!\n");
  441.     }
  442.  
  443.     checkabort(gui);
  444. }
  445.  
  446. AGLOBAL void eol_loop(ULONG class, struct Gadget* addr, UWORD code, UWORD qual)
  447. {   if (page == 71)
  448.     {   if (class == IDCMP_VANILLAKEY)
  449.         {   code = toupper(code);
  450.             if (code == 'S')
  451.             {   if (!(qual & IEQUALIFIER_LSHIFT) && !(qual & IEQUALIFIER_RSHIFT))
  452.                 {   if (eol.source < 2)
  453.                         eol.source++;
  454.                     else eol.source = 0;
  455.                 } else
  456.                 {   if (eol.source > 0)
  457.                         eol.source--;
  458.                     else eol.source = 2;
  459.                 }
  460.                 GT_SetGadgetAttrs
  461.                 (   MX71_Source,
  462.                     MainWindowPtr,
  463.                     NULL,
  464.                     GTMX_Active, eol.source,
  465.                     TAG_DONE
  466.                 );
  467.             } elif (code == 'D')
  468.             {   if (!(qual & IEQUALIFIER_LSHIFT) && !(qual & IEQUALIFIER_RSHIFT))
  469.                 {   if (eol.dest < 2)
  470.                         eol.dest++;
  471.                     else eol.dest = 0;
  472.                 } else
  473.                 {   if (eol.dest > 0)
  474.                         eol.dest--;
  475.                     else eol.dest = 2;
  476.                 }
  477.                 GT_SetGadgetAttrs
  478.                 (   MX71_Dest,
  479.                     MainWindowPtr,
  480.                     NULL,
  481.                     GTMX_Active, eol.dest,
  482.                     TAG_DONE
  483.                 );
  484.             } elif (code == 'O')
  485.             {   if (CB71_Detabulate->Flags & GFLG_SELECTED)
  486.                 {   eol.detabulate = FALSE;
  487.                 } else eol.detabulate = TRUE;
  488.                 updatedetabulate();
  489.             } elif (code == 'T')
  490.             {   ActivateGadget(IN71_TabSize, MainWindowPtr, NULL);
  491.             } elif (code == 'C')
  492.             {   eol_do();
  493.             } elif (code == 'P')
  494.             {   ActivateGadget(ST71_Pathname, MainWindowPtr, NULL);
  495.             } elif (code == ESCAPE)
  496.             {   page = 0;
  497.             } elif (code == '.')
  498.             {   multiasl("~(#?.info)");
  499.                 GT_SetGadgetAttrs
  500.                 (   ST71_Pathname,
  501.                     MainWindowPtr,
  502.                     NULL,
  503.                     GTST_String, shared.pathname,
  504.                     TAG_DONE
  505.                 );
  506.         }   }
  507.         elif (class == IDCMP_GADGETDOWN)
  508.         {   // radio gadgets
  509.             if (addr == MX71_Source)
  510.                 eol.source = code;
  511.             elif (addr == MX71_Dest)
  512.                 eol.dest = code;
  513.         } elif (class == IDCMP_GADGETUP)
  514.         {   // button, etc. gadgets
  515.             if (addr == BU99_Right)
  516.                 page = 0;
  517.             elif (addr == BU71_Convert)
  518.             {   eol_do();
  519.             } elif (addr == BU71_PathnameASL)
  520.             {   multiasl("~(#?.info)");
  521.                 GT_SetGadgetAttrs
  522.                 (   ST71_Pathname,
  523.                     MainWindowPtr,
  524.                     NULL,
  525.                     GTST_String, shared.pathname,
  526.                     TAG_DONE
  527.                 );
  528.             } elif (addr == CB71_Detabulate)
  529.             {   if (CB71_Detabulate->Flags & GFLG_SELECTED)
  530.                 {   eol.detabulate = TRUE;
  531.                 } else eol.detabulate = FALSE;
  532.                 updatedetabulate();
  533.     }   }   }
  534.     else
  535.     {   // assert(page == 72);
  536.         if (class == IDCMP_GADGETUP)
  537.         {   if (addr == BU99_Right)
  538.                 page = 0;
  539.         } elif (class == IDCMP_VANILLAKEY)
  540.         {   if (code == ESCAPE)
  541.                 page = 0;
  542. }   }   }
  543.  
  544. MODULE void updatedetabulate(void)
  545. {   GT_SetGadgetAttrs
  546.     (   CB71_Detabulate,
  547.         MainWindowPtr, NULL,
  548.         GTCB_Checked, eol.detabulate,
  549.         TAG_DONE
  550.     );
  551.     if (eol.detabulate)
  552.     {   GT_SetGadgetAttrs
  553.         (   IN71_TabSize,
  554.             MainWindowPtr, NULL,
  555.             GA_Disabled, FALSE,
  556.             TAG_DONE
  557.         );
  558.     } else
  559.     {   GT_SetGadgetAttrs
  560.         (   IN71_TabSize,
  561.             MainWindowPtr, NULL,
  562.             GA_Disabled, TRUE,
  563.             TAG_DONE
  564.         );
  565. }   }
  566.  
  567.